//index.js
const app = getApp()
var service = require('../../util/service')
Page({
  data: {
    head: {
        currentCity: '',
        placestr: '找品牌或商品'
    },
    tabbar: {},
    orgTypeGroup:[],
    swiperImgs:{},
    imgurl: service.imgurl,
    orgList: [],
    lat:0,
    lon:0,
    additions:"",
    lodding:false,
    loddtext:"加载中……",
    selOrg:1,
    pageSize: 10,
    page: 1,
    issq:false
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that= this;
    //调用app中的函数
    app.changeTabBar(); 
    app.choiceLogin(function (user) {
      if (user && user.pic && user.pic != "") {
        that.setData({
          'issq': false
        })
      }else{
        that.setData({
          'issq': true
        })
      }
    })
    service.AJAX("smc/sys/LookupValueLine/listLookupValueLine.json", { "$valHdsCode": "mainImages"},res => {
      let rows = res;
      for (var i = 0; i < rows.length;i++){
        rows[i].address = service.imgurl+rows[i].valLnsResult;
      }
      that.setData({
        'swiperImgs': rows
      })
    },err => {},'POST')

    service.AJAX("/smc/base/SystemProviderType/listSystemProviderType.json", {}, res => {
      var orgTypes = res;
      var orgTypeGroup = [];
      for (var i = 0, len = orgTypes.length; i < len; i += 8) {
        orgTypeGroup.push(orgTypes.slice(i, i + 8));
      }
      that.setData({
        'orgTypeGroup': orgTypeGroup
      })
    }, err => { })

    
    
  },
  getDistance: function (lat1, lng1, lat2, lng2) {
    lat1 = lat1 || 0;
    lng1 = lng1 || 0;
    lat2 = lat2 || 0;
    lng2 = lng2 || 0;
    var rad1 = lat1 * Math.PI / 180.0;
    var rad2 = lat2 * Math.PI / 180.0;
    var a = rad1 - rad2;
    var b = lng1 * Math.PI / 180.0 - lng2 * Math.PI / 180.0;
    var r = 6378137;
    return ((r * 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(rad1) * Math.cos(rad2) * Math.pow(Math.sin(b / 2), 2)))) / 1000).toFixed(1)
  },
  setOrgList:function(){
    var that = this;
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        that.setData({
          lat: res.latitude,
          lon: res.longitude,
        });
        that.getProvs();
      }
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  getProvs:function (){
    var that = this;
    that.setData({
      lodding: true,
      loddtext: "加载中……"
    })
    service.AJAX("smc/base/SystemProviderUser/querySystemProviderUser.json", { page: that.data.page, pageSize: that.data.pageSize, x: that.data.lat, y: that.data.lon, "$sortType-additions": that.data.additions, "$attribute3-notequal": "SP", "$attribute8": that.data.head.currentCity }, res => {
      var orgList = res.rows||[];
      var lodd = orgList.length < 10;
      console.log(orgList);
      for (var i = 0; i < orgList.length; i++) {
        var price = orgList[i].minPrice + "";
        var activitys = orgList[i].prodLabel;
        if (activitys && activitys != "") {
          orgList[i].activity = activitys.split(/[；;]/);
        }
        var prvDesc = orgList[i].prvDesc;
        if (prvDesc && prvDesc.length > 30 ){
          orgList[i].prvDesc = prvDesc.substr(0,30)+"……";
        }
        orgList[i].attribute1 = service.imgurl + orgList[i].attribute1;
        orgList[i].minPrice = price.indexOf(".") > 0 ? price.split(".") : (price + ".00").split(".");
        orgList[i].distance = that.getDistance(that.data.lat, that.data.lon, orgList[i].posX, orgList[i].posY);
      }
      var data = that.data.orgList.concat(orgList);
      that.setData({
        orgList: data,
        lodding: lodd,
        loddtext: lodd ? "没有更多数据了" :"加载中……"
      })
    }, err => { }, 'POST')
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    app.getpol(
        function (currentCity) {
          that.setData({
            'head.currentCity': currentCity,
            'page': 1,
            orgList: []
          })
          that.setOrgList();
        }
    )
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  selindex:function(){
    this.setData({
      'selOrg': 1,
      'page': 1,
      orgList:[],
      additions:""
    });
    this.getProvs();
  },
  selindex2: function () {
    this.setData({
      'selOrg': 2,
      'page': 1,
      orgList: [],
      additions: "LOOK"
    })
    this.getProvs();
  },
  selindex3: function () {
    this.setData({
      'selOrg': 3,
      'page': 1,
      orgList: [],
      additions: "SCORE"
    })
    this.getProvs();
  },
  lower:function(){
    this.setData({
      'page': this.data.page +1
    })
    this.getProvs();
  },
  onGotUserInfo(e) {
    var that = this;
    if (e.detail.userInfo){
      that.setData({
        'issq': false
      })
      app.setUserInfo(e.detail.userInfo, function (user) { });
    }
  },
  navigation: function (e) {
    var $this = this;
    var index = e.target.dataset.index;
    var data = $this.data.orgList[index];
    if (data.posX != "" && data.posY != "") {
      wx.openLocation({
        longitude: parseFloat(data.posY),
        latitude: parseFloat(data.posX),
        name: data.prvName,
        address: data.addrDetail,
        scale: 28
      })
    }
  },
})
